#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import os

class FileCheck():

    def __init__(self, filename:str, check_hash=False):
        self.filename = filename
        self.check_hash = check_hash
        self.mtime = os.path.getmtime(self.filename)

        self.hashv = 0
        with open(self.filename, 'r', encoding='utf-8') as fd:
            self.hashv = hash(fd.read())

        self.__count_changed = 0

    @property
    def count_changed(self):
        return self.__count_changed

    def changed(self):
        mtime = os.path.getmtime(self.filename)
        if mtime <= self.mtime:
            return False
        self.mtime = mtime

        if self.check_hash:
            with open(self.filename, 'r', encoding='utf-8') as fd:
                hashv = hash(fd.read())
                if hashv == self.hashv:
                    return False
                self.hashv = hashv

        self.__count_changed += 1
        return True
